﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using SQLite;

namespace MOJsdk
{
    public class MOJBasicFileClass
    {
        [PrimaryKey, AutoIncrement]
        public int FileID
        { get; set; }

        public string FilePath
        { get; set; }
        public string DiskID
        { get; set; }
        public int FileIndex
        { get; set; }
        public int FileType
        { get; set; }
        public string MovieCode
        { get; set; }

        public bool IsAvaible
        {
            get
            {
                if (!GlobalVars.TheHost.DrivePool.ContainsKey(DiskID))
                    return false;
                else
                    return true;
            }
        }
        public string FileFullPath
        {
            get
            {
                if (IsAvaible)
                {
                    return GlobalVars.TheHost.DrivePool[DiskID] + FilePath.Substring(1);
                }
                else
                    return "?" + FilePath.Substring(1);
            }
        }
        public MovieFileInfo Info
        {
            get
            {
                if (IsAvaible)
                    return new MovieFileInfo(FileFullPath);
                else
                    return null;
            }
        }

        public int ISO639_Code 
        { get; set; }
        public string Details
        { get; set; }

        public void UpdateInfo()
        {
            using (SQLite.SQLiteConnection _cn = new SQLiteConnection(GlobalVars.TheHost.SystemVars.DataBaseLocal))
            {
                _cn.Update(this, typeof(MOJBasicFileClass));
            }
        }

        public static void AddNewMojBasicFile(string PATH, MOJFileTypes TYPE, string MOVIECODE, int ISOCODE = 0)
        {
            MOJBasicFileClass file = null;

            switch (TYPE)
            {
                case MOJFileTypes.ImageFile:
                    file = new MOJBasicFileClass()
                    {
                        FilePath = PATH,
                        FileIndex = 0,
                        FileType = 2,
                        DiskID = TOOLS.GetDriveID(PATH),
                        MovieCode = MOVIECODE,
                    };
                    break;

                case MOJFileTypes.MovieFile:
                    file = new MOJBasicFileClass()
                    {
                        FilePath = PATH,
                        FileIndex = GlobalVars.TheHost.LoadMovie(MOVIECODE).MovieFiles.Count + 1,
                        FileType = 0,
                        DiskID = TOOLS.GetDriveID(PATH),
                        MovieCode = MOVIECODE,
                    };
                    break;

                case MOJFileTypes.SubtitleFile:
                    file = new MOJBasicFileClass()
                    {
                        FilePath = PATH,
                        FileIndex = 1,
                        FileType = 1,
                        DiskID = TOOLS.GetDriveID(PATH),
                        MovieCode = MOVIECODE,
                        ISO639_Code = ISOCODE,
                    };
                    break;
            }

            GlobalVars.TheHost.AddNewFileToDataBase(file);
        }
    }
    public enum MOJFileTypes
    {
        MovieFile = 0,
        SubtitleFile = 1,
        ImageFile = 2,
    }
}

